/**
 * aui-pull-refresh.js
 * @author 流浪男
 * @todo more things to abstract, e.g. Loading css etc.
 * Licensed under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 */
(function (window) {
    "use strict";
    /**
     * Extend obj function
     *
     * This is an object extender function. It allows us to extend an object
     * by passing in additional variables and overwriting the defaults.
     */
    var auiPullToRefresh = function (params, callback) {
        this.extend(this.params, params);
        this._init(callback);
    };
    var touchYDelta;
    var isLoading = false;
    var docElem = window.document.documentElement,
        loadWrapH,
        win = { width: window.innerWidth, height: window.innerHeight },
        winfactor = 0.2,
        translateVal,
        isMoved = false,
        firstTouchY,
        initialScroll;
    auiPullToRefresh.prototype = {
        params: {
            container: document.querySelector(".aui-refresh-content"),
            friction: 2.5,
            triggerDistance: 100,
            callback: false,
        },
        _init: function (callback) {
            var self = this;
            var loadingHtml =
                '<div class="aui-refresh-load"><div class="aui-refresh-pull-arrow"></div></div>';
            self.params.container.insertAdjacentHTML("afterbegin", loadingHtml);
            self.params.container.addEventListener("touchstart", function (ev) {
                self.touchStart(ev);
            });
            self.params.container.addEventListener("touchmove", function (ev) {
                self.touchMove(ev);
            });
            self.params.container.addEventListener("touchend", function (ev) {
                self.touchEnd(ev, callback);
            });
        },
        touchStart: function (ev) {
            // this.params.container.classList.remove("refreshing");
            if (isLoading) {
                return;
            }
            isMoved = false;
            this.params.container.style.webkitTransitionDuration =
                this.params.container.style.transitionDuration = "0ms";
            touchYDelta = "";
            var touchobj = ev.changedTouches[0];
            // register first touch "y"
            firstTouchY = parseInt(touchobj.clientY);
            initialScroll = this.scrollY();
        },
        touchMove: function (ev) {
            if (isLoading) {
                ev.preventDefault();
                return;
            }
            var self = this;
            var moving = function () {
                var touchobj = ev.changedTouches[0], // reference first touch point for this event
                    touchY = parseInt(touchobj.clientY);
                touchYDelta = touchY - firstTouchY;
                if (self.scrollY() === 0 && touchYDelta > 0) {
                    ev.preventDefault();
                }
                if (
                    initialScroll > 0 ||
                    self.scrollY() > 0 ||
                    (self.scrollY() === 0 && touchYDelta < 0)
                ) {
                    firstTouchY = touchY;
                    return;
                }
                translateVal = Math.pow(touchYDelta, 0.85);
                self.params.container.style.webkitTransform =
                    self.params.container.style.transform =
                        "translate3d(0, " + translateVal + "px, 0)";
                isMoved = true;
                if (touchYDelta > self.params.triggerDistance) {
                    self.params.container.classList.add("aui-refresh-pull-up");
                    self.params.container.classList.remove(
                        "aui-refresh-pull-down",
                    );
                } else {
                    self.params.container.classList.add(
                        "aui-refresh-pull-down",
                    );
                    self.params.container.classList.remove(
                        "aui-refresh-pull-up",
                    );
                }
            };
            this.throttle(moving(), 20);
        },
        touchEnd: function (ev, callback) {
            var self = this;
            if (isLoading || !isMoved) {
                isMoved = false;
                return;
            }
            // 根据下拉高度判断是否加载
            if (touchYDelta >= this.params.triggerDistance) {
                isLoading = true; //正在加载中
                ev.preventDefault();
                this.params.container.style.webkitTransitionDuration =
                    this.params.container.style.transitionDuration = "300ms";
                this.params.container.style.webkitTransform =
                    this.params.container.style.transform =
                        "translate3d(0,60px,0)";
                document.querySelector(
                    ".aui-refresh-pull-arrow",
                ).style.webkitTransitionDuration = document.querySelector(
                    ".aui-refresh-pull-arrow",
                ).style.transitionDuration = "0ms";
                self.params.container.classList.add("aui-refreshing");
                if (callback) {
                    callback({
                        status: "success",
                    });
                }
            } else {
                this.params.container.style.webkitTransitionDuration =
                    this.params.container.style.transitionDuration = "300ms";
                this.params.container.style.webkitTransform =
                    this.params.container.style.transform =
                        "translate3d(0,0,0)";
                if (callback) {
                    callback({
                        status: "fail",
                    });
                }
            }
            isMoved = false;
            return;
        },
        cancelLoading: function () {
            var self = this;
            isLoading = false;
            self.params.container.classList.remove("aui-refreshing");
            document.querySelector(
                ".aui-refresh-pull-arrow",
            ).style.webkitTransitionDuration = document.querySelector(
                ".aui-refresh-pull-arrow",
            ).style.transitionDuration = "300ms";
            this.params.container.style.webkitTransitionDuration =
                this.params.container.style.transitionDuration = "0ms";
            self.params.container.style.webkitTransform =
                self.params.container.style.transform = "translate3d(0,0,0)";
            self.params.container.classList.remove("aui-refresh-pull-up");
            self.params.container.classList.add("aui-refresh-pull-down");
            return;
        },
        scrollY: function () {
            return window.pageYOffset || docElem.scrollTop;
        },
        throttle: function (fn, delay) {
            var allowSample = true;
            return function (e) {
                if (allowSample) {
                    allowSample = false;
                    setTimeout(function () {
                        allowSample = true;
                    }, delay);
                    fn(e);
                }
            };
        },
        winresize: function () {
            var resize = function () {
                win = { width: window.innerWidth, height: window.innerHeight };
            };
            throttle(resize(), 10);
        },
        extend: function (a, b) {
            for (var key in b) {
                if (b.hasOwnProperty(key)) {
                    a[key] = b[key];
                }
            }
            return a;
        },
    };
    window.auiPullToRefresh = auiPullToRefresh;
})(window);
